package com.sjzd.entity;

import com.sjzd.ConfigParser;
import com.sjzd.menu.GameMenu;

import java.awt.*;
import java.util.Random;

public abstract class AbstractBase {
    public ConfigParser configParser = ConfigParser.getInstance();
    public GameMenu gameMenu;
    // 目前坐标
    public int X;
    public int Y;
    // 鼠标点击的目标坐标
    public int targetX;
    public int targetY;
    // 目前坐标与目标坐标之间的三角函数关系
    public double sinX;
    public double sinY;
    // 速度
    public int speed;
    // 存活状态
    public boolean isExist;
    // 轮廓
    public Rectangle rectangle;


    public AbstractBase(int X, int Y, int speed) {
        this(null, X, Y, 0.00, 1.00, speed);
    }

    public AbstractBase(GameMenu gameMenu, int X, int Y, int speed) {
        this(gameMenu, X, Y, 0.00, 1.00, speed);
    }

    public AbstractBase(int X, int Y, double sinX, double sinY,int speed) {
        this(null, X, Y, sinX, sinY, speed);
    }

    public AbstractBase(GameMenu gameMenu, int X, int Y, double sinX, double sinY,int speed) {
        this.gameMenu = gameMenu;
        this.X = X;
        this.Y = Y;
        targetX = -1;
        targetY = -1;
        this.sinX = sinX;
        this.sinY = sinY;
        this.speed = speed;
        isExist = true;
        rectangle = new Rectangle();
    }



    /**
     *  此方法设计好目标坐标后，会自动计算两点之间连线与水平线所成夹角的正弦和余弦值，
     *  注意为了避免代码冗余，只提供了计算正弦的方法；
     *  （此代码在com.sjzd.entity.Tank中进行了重写，原因是参与计算的起点坐标应该是
     *   Tank的中心坐标）
     * @param targetX
     * @param targetY
     */
    public void changeTarget(int targetX, int targetY) {
        this.targetX = targetX;
        this.targetY = targetY;
        sinX = calculateSin(X, Y, targetX, targetY);
        sinY = calculateSin(Y, X, targetY, targetX);
    }

    /**
     *  子类根据需要重写该方法
     * @throws Exception
     */
    public void move() throws Exception {
        throw new Exception("没有实现该方法");
    }


    // 计算角度
    public Double calculateSin(int X, int Y, int targetX, int targetY) {
        return Math.sin(Math.atan2(targetX - X, targetY - Y));
    }

    abstract void draw(AbstractBase abstractBase, ConfigParser configParser, Graphics g) throws Exception;
    abstract Rectangle getRectangle();
}
